package com.globagames.strategicgames.util
{
	import flash.display.BitmapData;
	import flash.filters.BitmapFilter;
	import flash.filters.BlurFilter;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.core.BitmapAsset;
	
	public class SoldierDesigner
	{
		[Embed(source="/assets/cercles.png")]
		private var cerclesClass:Class;
		
		private var bitmap:BitmapData;
		public function SoldierDesigner()
		{
			var ba:BitmapAsset = new cerclesClass() as BitmapAsset;
			bitmap = ba.bitmapData;
		}
		public function getNeutralBitmap(isCaptain:Boolean,bd:BitmapData):BitmapData {
			var retBd:BitmapData = new BitmapData(bd.width,bd.height,true,0x00000000);			
			retBd.copyPixels(bitmap, new Rectangle(0,0,128,64),new Point(0, bd.height -64));
			retBd.copyPixels(bd,bd.rect,new Point(0,0),null,null,true);
			if( isCaptain ) retBd.copyPixels(bitmap, new Rectangle(0,64,16,16),new Point(64-8, bd.height -32),null,null,true);
			return retBd;
		}
		public function getSelectedNeutralBitmap(isCaptain:Boolean, bd:BitmapData):BitmapData {
			var retBd:BitmapData = new BitmapData(bd.width,bd.height,true,0x00000000);			
			retBd.copyPixels(bitmap, new Rectangle(128,0,128,64),new Point(0, bd.height -64));
			retBd.copyPixels(bd,bd.rect,new Point(0,0),null,null,true);
			if( isCaptain ) retBd.copyPixels(bitmap, new Rectangle(0,64,16,16),new Point(64-8, bd.height -32),null,null,true);
			return retBd;
		}
		public function getActionSelectedNeutralBitmap(isCaptain:Boolean, bd:BitmapData):BitmapData {
			var retBd:BitmapData = new BitmapData(bd.width,bd.height,true,0x00000000);			
			retBd.copyPixels(bitmap, new Rectangle(256,0,128,64),new Point(0, bd.height -64));
			retBd.copyPixels(bd,bd.rect,new Point(0,0),null,null,true);
			if( isCaptain ) retBd.copyPixels(bitmap, new Rectangle(0,64,16,16),new Point(64-8, bd.height -32),null,null,true);
			return retBd;
		}
		
		public function grayscale(bd:BitmapData):BitmapData {
			
			var grayBd:BitmapData = new BitmapData(bd.rect.width, bd.rect.height,true, 0x00000000);
			var blurry:BlurFilter = new BlurFilter(10,10);
	        var edge:BitmapFilter = convertiGrigio(); 
	        grayBd.applyFilter(bd,bd.rect,new Point(0,0),edge);
	        return grayBd;
						            
		}
		
		private function convertiGrigio():BitmapFilter
		{
			var r:Number=0.212671;
			var g:Number=0.715160;
			var b:Number=0.072169;
			return new ColorMatrixFilter
			(
				[r,g,b,0,0,
				r,g,b,0,0,
				r,g,b,0,0,
				0,0,0,1,0]   );
		}

	}
}